package com.dbkj.axb_relation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dbkj.axb_a.AxbAService;
import com.dbkj.axb_group.AxbGroupRelationService;
import com.dbkj.common.model.AxbRelation;
import com.dbkj.common.utils.RedisUtils;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.Restful;
import com.jfinal.json.Json;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.render.JsonRender;

import java.util.List;

/**
 * @Author: Max
 * 组
 * @Date: 2019/10/29 9:23
 */


public class AxbRelationController extends Controller {

    @Inject
    AxbRelationService service;

    @Inject
    AxbAService axbAService;

    @Inject
    AxbGroupRelationService axbGroupRelationService;
    /**
     *组的展示
     */
    @Before(Restful.class)
    public void index(){
        Integer pageNumber = getParaToInt("pageNumber");
        Integer pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        Page<AxbRelation> page = service.paginate(pageNumber,pageSize,name);

        List<AxbRelation> list = page.getList();
        for(AxbRelation r:list){
            String relation = r.getARelation();
            Long len = 0L;
            if(PropKit.getBoolean("is_cluster")){
                len = RedisUtils.cluster.llen("relation_"+relation+"_concurrent_list");
            }else{
                len = RedisUtils.jedis.llen("relation_"+relation+"_concurrent_list");
            }
            r.setCurrentR(len);
        }
        renderJson(Ret.ok().set("total",page.getTotalRow()).set("rows", JSON.toJSON(page.getList())));
    }

    /**
     * 组的添加
     */
    @Before(Restful.class)
    public void save(){
        AxbRelation r = Json.getJson().parse(getRawData(),AxbRelation.class);
        r.save();
        if(StrKit.notBlank(r.getConcurrent())){
            if(PropKit.getBoolean("is_cluster")){
                RedisUtils.cluster.del("relation_"+r.getARelation()+"_concurrent_list");
                for(int i=0;i<Integer.parseInt(r.getConcurrent());i++){
                    RedisUtils.cluster.lpush("relation_"+r.getARelation()+"_concurrent_list","1");
                }
            }else{
                RedisUtils.jedis.del("relation_"+r.getARelation()+"_concurrent_list");
                for(int i=0;i<Integer.parseInt(r.getConcurrent());i++){
                    RedisUtils.jedis.lpush("relation_"+r.getARelation()+"_concurrent_list","1");
                }
            }

        }
        renderJson(Ret.ok());
    }

    public void current(){
        JSONArray array = JSONArray.parseArray(getRawData());
        for(int i=0;i<array.size();i++){
            JSONObject object = array.getJSONObject(i);
            String aRelation = object.getString("aRelation");
            Integer currentROld = object.getInteger("currentROld");
            Integer currentR = object.getInteger("currentR");
            if(PropKit.getBoolean("is_cluster")){
                if(currentR < currentROld){
                    for(int j=0;j<currentROld-currentR;j++){
                        RedisUtils.cluster.rpop("relation_"+aRelation+"_concurrent_list");
                    }
                }else if(currentR > currentROld){
                    for(int j=0;j<currentR-currentROld;j++){
                        RedisUtils.cluster.lpush("relation_"+aRelation+"_concurrent_list","1");
                    }
                }
            }else {
                if(currentR < currentROld){
                    for(int j=0;j<currentROld-currentR;j++){
                        RedisUtils.jedis.rpop("relation_"+aRelation+"_concurrent_list");
                    }
                }else if(currentR > currentROld){
                    for(int j=0;j<currentR-currentROld;j++){
                        RedisUtils.jedis.lpush("relation_"+aRelation+"_concurrent_list","1");
                    }
                }
            }

        }
        renderJson(Ret.ok());
    }

    /**
     * 组的更新
     */
    @Before(Restful.class)
    public void update(){
        AxbRelation relation = Json.getJson().parse(getRawData(),AxbRelation.class);
        relation.update();
        renderJson(Ret.ok());
    }

    /**
     * 组的删除
     */
    @Before(Restful.class)
    public void delete(){
        Integer id = getParaToInt();

        if(axbAService.hasRelation(id)){
            renderError(401,new JsonRender(Ret.by("msg","有相关联的A号码")));
        }else{
            if(axbGroupRelationService.hasRelation(id)){
                renderError(401, new JsonRender(Ret.by("msg","有相关联的群")));
            } else{
                service.delete(id);
                renderJson(Ret.ok());
            }
        }
    }

    public void checkFlag(){
        JSONObject json = JSON.parseObject(getRawData());
        Integer id = json.getInteger("id");
        Boolean checkFlag = json.getBoolean("checkFlag");
        service.checkFlag(id,checkFlag);
        renderJson(Ret.ok());
    }
}
